package com.youlu.campus.service.ca;

import cn.hutool.extra.pinyin.PinyinUtil;
import com.youlu.campus.entity.ActivityInfo;
import com.youlu.campus.entity.UserInfoExtend;
import com.youlu.campus.service.activity.ActivityService;
import com.youlu.campus.service.visitor.UserInfoExtendService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 拼音服务类
 */
@Slf4j
@Service
public class CaPinYinService {

    @Lazy
    @Autowired
    private ActivityService activityService;

    //复性别
    private static final String[] COMPOUND_SURNAME = {
            "欧阳", "太史", "端木", "上官", "司马", "东方", "独孤", "南宫", "万俟", "闻人", "夏侯", "诸葛",
            "尉迟", "公羊", "赫连", "澹台", "皇甫", "宗政", "濮阳", "公冶", "太叔", "申屠", "公孙", "慕容",
            "仲孙", "钟离", "长孙", "宇文", "司徒", "鲜于", "司空", "闾丘", "子车", "亓官", "司寇", "巫马",
            "公西", "颛孙", "壤驷", "公良", "漆雕", "乐正", "宰父", "谷梁", "拓跋", "夹谷", "轩辕", "令狐", "段干", "百里", "呼延", "东郭", "南门",
            "羊舌", "微生", "公户", "公玉", "公仪", "梁丘", "公仲", "公上", "公门", "公山", "公坚", "左丘", "公伯",
            "西门", "公祖", "第五", "公乘", "贯丘", "公皙", "南荣", "东里", "东宫", "仲长", "子书", "子桑", "即墨", "达奚", "褚师"};

    /**
     * 获取当前报名报名拼音
     *
     * @param activityId
     * @param userInfo
     * @return
     */
    public String getPinYinNameByUserId(String activityId, UserInfoExtend userInfo) {
        ActivityInfo activityInfo = activityService.findOne(activityId);
        if (Objects.isNull(activityInfo) || !ActivityInfo.DIGITAL_ENGLISH_CA.equals(activityInfo.getDigitalCaType())) {
            log.warn("【领取证书-姓名转拼音】活动不支持英文版本");
            return "";
        }
        log.info("【领取证书-姓名转拼音】start 生成当前用户->{}拼音 | activityId - >{}", userInfo.getUserId(), activityId);
        if (StringUtils.isNotBlank(userInfo.getEnName())) {
            log.info("【领取证书】end 生成当前用户->{}拼音 | activityId - >{} | pinyin ->{}", userInfo.getUserId(), activityId,
                    userInfo.getEnName());
            return userInfo.getEnName();
        }
        String chineseName = userInfo.getName();
        if (StringUtils.isBlank(chineseName)) {
            log.warn("【领取证书-姓名转拼音】end 生成当前用户->{}拼音 | activityId - >{}  姓名为空", userInfo.getUserId(), activityId);
            return "";
        }
        String targetPinYinName = "";
        log.info("【领取证书-姓名转拼音】start 生成当前用户->{}拼音 | activityId - >{} 中文名名称 ", userInfo.getUserId(), activityId,
                chineseName);
        if (chineseName.length() >= 3) {
            //新疆地区姓名
            String pinyinXing = "";
            String pinyinMing = "";
            String xing = "";
            String ming = "";
            if (chineseName.indexOf("·") > 0) {
                xing = chineseName.split("·")[0];
                ming = chineseName.split("·")[1];
                log.info("【领取证书-姓名转拼音】start 生成当前用户->{}拼音 | activityId - >{} 新疆地区姓 ->{} |名 ->{}", userInfo.getUserId()
                        , activityId, xing, ming);
                pinyinXing = PinyinUtil.getPinyin(xing, "");
                pinyinMing = PinyinUtil.getPinyin(ming, "");
                log.info("【领取证书-姓名转拼音】start 生成当前用户->{}拼音 | activityId - >{} 新疆地区拼音姓 ->{} | 拼音名 ->{}",
                        userInfo.getUserId(), activityId, pinyinXing, pinyinMing);
                targetPinYinName = convertToPinYin(pinyinXing + " " + pinyinMing);
                log.info("【领取证书-姓名转拼音】start 生成当前用户->{}拼音 | activityId - >{} 新疆地区拼音转后拼音 ->{} ", userInfo.getUserId(),
                        activityId, targetPinYinName);
                return targetPinYinName;
            }
            //是否存在复姓情况
            String compoundSurname = Arrays.stream(COMPOUND_SURNAME).collect(Collectors.joining("|"));
            boolean compoundSurnameMatches = Pattern.matches(".*(" + compoundSurname + ").*", chineseName);
            //存在姓
            int length = chineseName.length();
            if (compoundSurnameMatches) {
                String compoundSurnameXing = chineseName.substring(0, 2);
                String compoundSurnameMing = chineseName.substring(2, length);
                log.info("【领取证书-姓名转拼音】 生成当前用户->{}拼音 | activityId - >{} 复姓 ->{} | 复姓名 ->{}", userInfo.getUserId(),
                        activityId, compoundSurnameXing, compoundSurnameMing);
                pinyinXing = PinyinUtil.getPinyin(compoundSurnameXing, "");
                pinyinMing = PinyinUtil.getPinyin(compoundSurnameMing, "");
                log.info("【领取证书-姓名转拼音】 生成当前用户->{}拼音 | activityId - >{} 拼音复姓 ->{} | 拼音复姓名 ->{}", userInfo.getUserId(),
                        activityId, pinyinXing, pinyinMing);
                targetPinYinName = convertToPinYin(pinyinXing + " " + pinyinMing);
                log.info("【领取证书-姓名转拼音】 生成当前用户->{}拼音 | activityId - >{} 拼音复姓 ->{} | 拼音复姓名 ->{} 结果拼接->{}",
                        userInfo.getUserId(), activityId, pinyinXing, pinyinMing, targetPinYinName);
                return targetPinYinName;
            }
            //不存在复姓
            xing = chineseName.substring(0, 1);
            ming = chineseName.substring(1, length);
            log.info("【领取证书-姓名转拼音】 生成当前用户->{}拼音 | activityId - >{} 姓 ->{} | 名 ->{}", userInfo.getUserId(), activityId
                    , xing, ming);
            pinyinXing = PinyinUtil.getPinyin(xing, "");
            pinyinMing = PinyinUtil.getPinyin(ming, "");
            log.info("【领取证书-姓名转拼音】 生成当前用户->{}拼音 | activityId - >{} 拼音姓 ->{} | 拼音姓名 ->{}", userInfo.getUserId(),
                    activityId, pinyinXing, pinyinMing);
            targetPinYinName = convertToPinYin(pinyinXing + " " + pinyinMing);
            log.info("【领取证书-姓名转拼音】 生成当前用户->{}拼音 | activityId - >{}  结果拼接->{}", userInfo.getUserId(), activityId,
                    targetPinYinName);
            return targetPinYinName;
        }
        //中文名只存在2个字情况
        String pinyinName = PinyinUtil.getPinyin(chineseName);
        log.info("【领取证书-姓名转拼音】start 生成当前用户->{}拼音 | activityId - >{} 拼音名名称 ->{} ", userInfo.getUserId(), activityId,
                pinyinName);
        targetPinYinName = convertToPinYin(pinyinName);
        log.info("【领取证书-姓名转拼音】start 生成当前用户->{}拼音 | activityId - >{} 拼音名名称 ->{} 转后拼音 ->{} ", userInfo.getUserId(),
                activityId, pinyinName, targetPinYinName);
        return targetPinYinName;
    }

    public static void main(String[] args) {
        String chineseName = "一颗萝卜";
        String xing = chineseName.substring(0, 1);
        String ming = chineseName.substring(1, chineseName.length());
        String pinyinXing = PinyinUtil.getPinyin(xing, "");
        String pinyinMing = PinyinUtil.getPinyin(ming, "");
        String targetPinYinName = convertToPinYin(pinyinXing + " " + pinyinMing);
       System.out.println(targetPinYinName);
    }

    private static String convertToPinYin(String originPinYin) {
        StringBuffer pinYinbf = new StringBuffer();
        Matcher m = Pattern.compile("([a-z])([a-z]*)", Pattern.CASE_INSENSITIVE).matcher(originPinYin);
        while (m.find()) {
            m.appendReplacement(pinYinbf, m.group(1).toUpperCase() + m.group(2).toLowerCase());
        }
        return m.appendTail(pinYinbf).toString();
    }
}
